import type { App, Plugin } from 'vue'

// 在 SSR 环境中为 Node.js 提供 FormData polyfill
// 这必须在任何其他导入之前执行
if (typeof globalThis !== 'undefined' && !globalThis.FormData) {
  class FormDataPolyfill {
    private data: Map<string, any> = new Map();

    append(name: string, value: any, filename?: string) {
      this.data.set(name, { value, filename });
    }

    get(name: string) {
      const entry = this.data.get(name);
      return entry ? entry.value : null;
    }

    has(name: string) {
      return this.data.has(name);
    }

    delete(name: string) {
      this.data.delete(name);
    }

    entries() {
      return this.data.entries();
    }

    keys() {
      return this.data.keys();
    }

    values() {
      return this.data.values();
    }

    [Symbol.iterator]() {
      return this.data.entries();
    }
  }

  (globalThis as any).FormData = FormDataPolyfill;
}

// 导入全局样式 - 确保样式被打包进去
import './styles/global.scss'

// 导入第三方库样式 - 现在将它们打包到组件库中
import 'swiper/css'
import 'swiper/css/navigation'
import 'swiper/css/pagination'
import 'swiper/css/effect-fade'
import '@splidejs/vue-splide/css'
import {
  ArticleList,
  BannerImage,
  BannerPage,
  BtnList,
  GalleryDefault,
  GalleryCaptionRound,
  GalleryTimeline,
  GalleryTimelineCol,
  Process,
  CustomSpace,
  GalleryList,
  HeroSlide,
  LinkList,
  QuoteText,
  DonorLinks,
  CardCarousel,
  CardCarouselMultiRow,
  BannerQuote,
  ArticleItem,
  RibbonBlock,
  TimelineStory,
  NewsList,
  TheHistoryCarousel,
  CardShowcaseProfile,
  CountDown,
  CustomIframe,
  ContactUsList,
  TableTwo,
  BannerImageWithLink,
  ScrollKeyInfo,
  ContactUsInfo,
  Publications,
  MapStatic,
  // CardCarouselArticles
  CarouselLogo,
  Resources,
  ResourcesTwo,
  Disc,
  BasicCollapse,
  TableCollapse,
  MediaLogo,
  RegisterForm,
  GalleryBlock,
  CardCarouselMultiRowArticles,
  // 新增组件
  AlummiCommunityList,
  BlessingMessage,
  BlessingSlider,
  CardShowcase,
  ExploreLinks,
  SchoolLayout,
  CardCarouselMultiRowPublications
 } from './components'

/**
 * 全量导出插件 - 用于 app.use(OfficialBlock)
 * 注意：这会注册所有组件，建议按需导入以减少包大小
 */
const OfficialBlock = {
  install: (app: App) => {
    // 注册设备检测全局属性（SSR 安全）
    app.config.globalProperties.$device = {
      get isMobile() {
        return typeof window !== 'undefined' ? window.innerWidth < 768 : false
      },
      get isTablet() {
        return typeof window !== 'undefined' ? window.innerWidth >= 768 && window.innerWidth < 1024 : false
      },
      get isMobileOrTablet() {
        return typeof window !== 'undefined' ? window.innerWidth < 1024 : false
      },
      get isDesktop() {
        return typeof window !== 'undefined' ? window.innerWidth >= 1024 : true
      }
    }

    // 注册组件
    app.component('ArticleList', ArticleList)
    app.component('BannerImage', BannerImage)
    app.component('BannerPage', BannerPage)
    app.component('BtnList', BtnList)
    app.component('GalleryDefault', GalleryDefault)
    app.component('GalleryCaptionRound', GalleryCaptionRound)
    app.component('GalleryTimeline', GalleryTimeline)
    app.component('GalleryTimelineCol', GalleryTimelineCol)
    app.component('Process', Process)
    app.component('CustomSpace', CustomSpace)
    app.component('GalleryList', GalleryList)
    app.component('HeroSlide', HeroSlide)
    app.component('LinkList', LinkList)
    app.component('QuoteText', QuoteText)
    app.component('DonorLinks', DonorLinks)
    app.component('CardCarousel', CardCarousel)
    app.component('CardCarouselMultiRow', CardCarouselMultiRow)
    app.component('BannerQuote', BannerQuote)
    app.component('ArticleItem', ArticleItem)
    app.component('RibbonBlock', RibbonBlock)
    app.component('TimelineStory', TimelineStory)
    app.component('NewsList', NewsList)
    app.component('TheHistoryCarousel', TheHistoryCarousel)
    app.component('CardShowcaseProfile', CardShowcaseProfile)
    app.component('CountDown', CountDown)
    app.component('ContactUsList', ContactUsList)
    app.component('CustomIframe', CustomIframe)
    app.component('TableTwo', TableTwo)
    app.component('ScrollKeyInfo', ScrollKeyInfo)
    app.component('BannerImageWithLink', BannerImageWithLink)
    app.component('ContactUsInfo', ContactUsInfo)
    app.component('Publications', Publications)
    app.component('MapStatic', MapStatic)
    // app.component('CardCarouselArticles', CardCarouselArticles)
    app.component('CarouselLogo', CarouselLogo)
    app.component('Resources', Resources)
    app.component('ResourcesTwo', ResourcesTwo)
    app.component('Disc', Disc)
    app.component('BasicCollapse', BasicCollapse)
    app.component('TableCollapse', TableCollapse)
    app.component('MediaLogo', MediaLogo)
    app.component('RegisterForm', RegisterForm)
    app.component('GalleryBlock', GalleryBlock)
    app.component('CardCarouselMultiRowArticles', CardCarouselMultiRowArticles)
    // 新增组件
    app.component('AlummiCommunityList', AlummiCommunityList)
    app.component('BlessingMessage', BlessingMessage)
    app.component('BlessingSlider', BlessingSlider)
    app.component('CardShowcase', CardShowcase)
    app.component('ExploreLinks', ExploreLinks)
    app.component('SchoolLayout', SchoolLayout)
    app.component('CardCarouselMultiRowPublications', CardCarouselMultiRowPublications)
  },
} satisfies Plugin

// 默认导出插件
export default OfficialBlock

// 按需引入 - 导出组件
export { 
  ArticleList, 
  BannerImage, 
  BannerPage, 
  BtnList,
  GalleryDefault,
  GalleryCaptionRound,
  GalleryTimeline,
  GalleryTimelineCol,
  Process,
  CustomSpace, 
  GalleryList,
  HeroSlide,
  LinkList,
  QuoteText,
  DonorLinks,
  CardCarousel,
  CardCarouselMultiRow,
  BannerQuote,
  ArticleItem,
  RibbonBlock,
  TimelineStory,
  NewsList,
  TheHistoryCarousel,
  CardShowcaseProfile,
  CountDown,
  CustomIframe,
  ContactUsList,
  TableTwo,
  BannerImageWithLink,
  ScrollKeyInfo,
  ContactUsInfo,
  Publications,
  MapStatic,
  // CardCarouselArticles
  CarouselLogo,
  Resources,
  ResourcesTwo,
  Disc,
  BasicCollapse,
  TableCollapse,
  MediaLogo,
  RegisterForm,
  GalleryBlock,
  CardCarouselMultiRowArticles,
  // 新增组件
  AlummiCommunityList,
  BlessingMessage,
  BlessingSlider,
  CardShowcase,
  ExploreLinks,
  SchoolLayout,
  CardCarouselMultiRowPublications
}

// 按需引入 - 导出单个组件的插件
export { 
  ArticleListPlugin, 
  BannerImagePlugin, 
  BannerPagePlugin, 
  BtnListPlugin, 
  GalleryDefaultPlugin,
  GalleryCaptionRoundPlugin,
  GalleryTimelinePlugin,
  GalleryTimelineColPlugin,
  ProcessPlugin,
  CustomSpacePlugin, 
  GalleryListPlugin,
  HeroSlidePlugin,
  LinkListPlugin, 
  QuoteTextPlugin,
  DonorLinksPlugin,
  CardCarouselPlugin,
  CardCarouselMultiRowPlugin,
  BannerQuotePlugin,
  ArticleItemPlugin,
  RibbonBlockPlugin,
  TimelineStoryPlugin,
  NewsListPlugin,
  TheHistoryCarouselPlugin,
  CardShowcaseProfilePlugin,
  CountDownPlugin,
  ContactUsListPlugin,
  CustomIframePlugin,
  TableTwoPlugin,
  BannerImageWithLinkPlugin,
  ScrollKeyInfoPlugin,
  ContactUsInfoPlugin,
  PublicationsPlugin,
  MapStaticPlugin,
  CardCarouselArticlesPlugin,
  CarouselLogoPlugin,
  ResourcesPlugin,
  ResourcesTwoPlugin,
  DiscPlugin,
  BasicCollapsePlugin,
  TableCollapsePlugin,
  MediaLogoPlugin,
  RegisterFormPlugin,
  GalleryBlockPlugin,
  CardCarouselMultiRowArticlesPlugin,
  // 新增组件插件
  AlummiCommunityListPlugin,
  BlessingMessagePlugin,
  BlessingSliderPlugin,
  CardShowcasePlugin,
  ExploreLinksPlugin,
  SchoolLayoutPlugin,
  CardCarouselMultiRowPublicationsPlugin
} from './components'

// 导出主题相关功能
export { useTheme } from './composables/useTheme'
export { ThemeUtils, ResponsiveUtils, AnimationUtils, StorageUtils } from './utils/theme'

// 导出类型定义
export type {
  ArticleListProps,
  ArticleListEmits,
  BannerImageProps,
  BannerImageEmits,
  BannerPageProps,
  BannerPageEmits,
  BtnListProps,
  BtnListEmits,
  GalleryDefaultProps,
  GalleryDefaultEmits,
  GalleryCaptionRoundProps,
  GalleryCaptionRoundEmits,
  GalleryTimelineProps,
  GalleryTimelineEmits,
  GalleryTimelineColProps,
  GalleryTimelineColEmits,
  ProcessProps,
  ProcessEmits,
  CustomSpaceProps,
  CustomSpaceEmits,
  GalleryListProps,
  GalleryListEmits,
  HeroSlideProps,
  HeroSlideEmits,
  LinkListProps,
  LinkListEmits,
  QuoteTextProps,
  QuoteTextEmits,
  DonorLinksProps,
  DonorLinksEmits,
  CardCarouselProps,
  CardCarouselEmits,
  CardCarouselMultiRowProps,
  CardCarouselMultiRowEmits,
  BannerQuoteProps,
  BannerQuoteEmits,
  ArticleItemProps,
  ArticleItemEmits,
  RibbonBlockProps,
  RibbonBlockEmits,
  TimelineStoryProps,
  TimelineStoryEmits,
  NewsListProps,
  NewsListEmits,
  TheHistoryCarouselProps,
  TheHistoryCarouselEmits,
  CardShowcaseProfileProps,
  CardShowcaseProfileEmits,
  CountDownProps,
  CountDownEmits,
  CustomIframeProps,
  CustomIframeEmits,
  ContactUsListProps,
  ContactUsListEmits,
  TableTwoProps,
  TableTwoEmits,
  BannerImageWithLinkProps,
  BannerImageWithLinkEmits,
  ScrollKeyInfoProps,
  ScrollKeyInfoEmits,
  ContactUsInfoProps,
  ContactUsInfoEmits,
  PublicationsProps,
  PublicationsEmits,
  MapStaticProps,
  MapStaticEmits,
  CardCarouselArticlesProps,
  CardCarouselArticlesEmits,
  CarouselLogoProps,
  CarouselLogoEmits,
  ResourcesProps,
  ResourcesEmits,
  ResourcesTwoProps,
  ResourcesTwoEmits,
  DiscProps,
  DiscEmits,
  BasicCollapseProps,
  BasicCollapseEmits,
  TableCollapseProps,
  TableCollapseEmits,
  MediaLogoProps,
  MediaLogoEmits,
  RegisterFormProps,
  RegisterFormEmits,
  GalleryBlockProps,
  GalleryBlockEmits,
  CardCarouselMultiRowArticlesProps,
  CardCarouselMultiRowArticlesEmits,
  // 新增组件类型
  AlummiCommunityListProps,
  AlummiCommunityListEmits,
  BlessingMessageProps,
  BlessingMessageEmits,
  BlessingSliderProps,
  BlessingSliderEmits,
  CardShowcaseProps,
  CardShowcaseEmits,
  ExploreLinksProps,
  ExploreLinksEmits,
  SchoolLayoutProps,
  SchoolLayoutEmits,
  CardCarouselMultiRowPublicationsProps,
  CardCarouselMultiRowPublicationsEmits
} from './components'

export type { ThemeConfig } from './composables/useTheme'